<?php if (!defined('THINK_PATH')) exit();?><section class="content-header">
	<h1>
		栏目管理
	</h1>
	<ol class="breadcrumb">
		<li><a href="<?php echo U('/Admin/index');?>"><i class="fa fa-home"></i>首页</a></li>
		<li class="active">栏目管理</li>
	</ol>
</section>
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<div class="box">
				<div class="box-header">
					<div class="box-title"><label>栏目列表</label></div>
					<div class="pull-right">
						<button class="btn btn-block btn-primary btn-sm" onclick="menuAdd()"><i class="fa fa-plus"></i>&nbsp;&nbsp;&nbsp;添加栏目</button>
					</div>
				</div><!-- /.box-header -->
				<div class="box-body">
					<table id="table" class="table table-bordered table-striped" width="100%">
						<thead>
						<tr>
							<th>栏目ID</th>
							<th>排序</th>
							<th>栏目名称</th>
							<th>栏目类型</th>
							<th>描述</th>
							<th>状态</th>
							<th>管理操作</th>
						</tr>
						</thead>
						<tbody id="dataBody"></tbody>
					</table>
				</div><!-- /.box-body -->
			</div><!-- /.box -->
		</div>
	</div>
</section>
<script type="text/javascript">
	$(function () {
		$('#table').dataTable({
			"autoWidth": true,
			"searching": false,
			"ordering": false,
			"paging": false,
			"language": lang
		});
		$('.dataTables_empty').remove();
		$('.dataTables_info').remove();
		$.getJSON("<?php echo U('Category/categoryListData');?>", function (json) {
			getChild(json, 0, 0);
			$('.tree-hit').each(function () {
				$(this).click(function () {
					if ($(this).hasClass('tree-collapsed')) {
						$(this).removeClass('tree-collapsed');
						$(this).addClass('tree-expanded');
						$(this).removeClass('tree-collapsed-hover');
						$(this).removeClass('tree-expanded-hover');
						$(this).parent().find('.tree-folder').addClass('tree-folder-open');
						var treeNode = $(this).attr("child");
						toggle($("." + treeNode), true);
					} else {
						$(this).removeClass('tree-expanded');
						$(this).addClass('tree-collapsed');
						$(this).removeClass('tree-collapsed-hover');
						$(this).removeClass('tree-expanded-hover')
						$(this).parent().find('.tree-folder').removeClass('tree-folder-open');
						var treeNode = $(this).attr("child");
						toggle($("." + treeNode), false);
					}
				});
				$(this).hover(function () {
					if ($(this).hasClass('tree-collapsed')) {
						$(this).addClass('tree-collapsed-hover');
					} else {
						$(this).addClass('tree-expanded-hover');
					}
				}, function () {
					if ($(this).hasClass('tree-collapsed')) {
						$(this).removeClass('tree-collapsed-hover');
					} else {
						$(this).removeClass('tree-expanded-hover');
					}
				})
			});
		});
	});

	function toggle(p, show) {
		p.each(function() {
			if(($(this).find('.tree-expanded').length>0&&show)||!show){
				var c = $("." + $(this).attr('child'));
				if (c.length > 0) {
					c.each(function () {
						toggle($(this), show);
					});
				}
			}
			if (show) {
				$(this).show();
			} else {
				$(this).hide();
			}
		})
	}

	function getChild(json, l, pid) {
		var space = '';
		for (var j = 0; j < l; j++) {
			space += '<span class="tree-indent"></span>';
		}
		l++;
		for (var i = 0; i < json.length; i++) {
			var data = json[i];
			var html = '<tr class="tree_' + pid + '"  child="tree_' + data.catid + '">' +
					'<td>' + data.catid + '</td>' +
					'<td><input type="text" style="text-align:center;height: 20px" size="2" value="' + data.listorder + '"  id="menu_order_input'+data.catid+'" onblur="menuOrder('+data.catid+')"></td>' +
					'<td><div class="datagrid-cell datagrid-cell-c1-name" style=";height:auto;">' + space;
			if (data.children.length > 0) {
				html += '<span class="tree-hit tree-expanded" child="tree_' + data.catid + '"></span><span class="tree-icon tree-folder tree-folder-open"></span>';
			} else {
				html += '<span class="tree-icon tree-file "></span>';
			}
			html += '<span class="tree-title">' + data.catname + '</span></div></td>';
			html +='<td><div class="datagrid-cell datagrid-cell-c1-operateid" >'+(data.type==0?"栏目":(data.type==1?"页面":"连接"))+'</div></td>';
			html +='<td><div class="datagrid-cell datagrid-cell-c1-operateid" >'+data.description+'</div></td>';
			html +='<td><div class="datagrid-cell datagrid-cell-c1-operateid" >'+(data.ismenu==1?"启用":"停用")+'</div></td>';
			html +='<td><div class="datagrid-cell datagrid-cell-c1-operateid" style="height:auto;">' +
					'<a onclick="menuAdd(' + data.catid + ')" href="javascript:;">添加子栏目</a>';
			if(data.is_system!=1){
				html+= ' | <a onclick="menuEdit(' + data.catid + ')" href="javascript:;">修改</a> | <a onclick="menuDelete(' + data.catid + ')" href="javascript:;">删除</a>';
			}
			html+='</div></td></tr>';
			$("#dataBody").append(html);
			if (data.children.length > 0) {
				getChild(data.children, l, data.catid);
			}
		}

	}

	function menuAdd(parentid){
		if(typeof(parentid) !== 'number') parentid = 0;
		var url = "<?php echo U('Category/categoryAdd');?>";
		url += url.indexOf('?') != -1 ? '&parentid='+parentid : '?parentid='+parentid;
		$.dialog(url,"添加菜单",null,46);
	}



	//编辑
	function menuEdit(id){
		if(typeof(id) !== 'number'){
			$.messager.alert('提示信息', '未选择栏目', 'error');
			return false;
		}
		var url = "<?php echo U('Category/categoryAdd');?>";
		url += url.indexOf('?') != -1 ? '&id='+id : '?id='+id;
		$.dialog(url,"修改栏目",null,46);
	}

	//删除
	function menuDelete(id){
		if(typeof(id) !== 'number'){
			$.messager.alert('提示信息', '未选择栏目', 'error');
			return false;
		}
		$.messager.confirm('提示信息', '确定要删除吗？', function(result){
			if(!result) return false;
			$.post("<?php echo U('Category/categoryDelete');?>", {id: id}, function(res){
				if(!res.status){
					$.messager.alert('提示信息', res.info, 'error');
				}else{
					$.messager.alert('提示信息', res.info, 'info');
					reloadContent();
				}
			}, 'json');
		});
	}
	//排序
	function menuOrder(id){
		$.post('<?php echo U("Category/categoryOrder");?>', "order="+$('#menu_order_input'+id).val()+"&id="+id, function(res){
			if(!res.status){
				$.messager.alert('提示信息', res.info, 'error');
			}else{
				$.messager.alert('提示信息', res.info, 'info');
				reloadContent();
			}
		}, 'json');
	}


</script>